package apis.amapv2.com.spannablestringspannablestringbuilder.activity;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Color;
import android.graphics.MaskFilter;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.MaskFilterSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.ScaleXSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.TextAppearanceSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import apis.amapv2.com.spannablestringspannablestringbuilder.R;

public class SpanActivity extends AppCompatActivity {

    private Context mContext;
    private TextView mTvForegroundColor;//设置颜色
    private TextView mTvBackgroundColor;//设置背景颜色
    private TextView mTvClickable;//可点击
    private TextView mTvBlueMaskFilter;//模糊效果
    private TextView mTvStrikeThrough;//删除线
    private TextView mTvUnderlineSpan;//下划线
    private TextView mTvAbsoluteSize;//字体绝对大小
    private TextView mTvDynamicDrawable;//图片，基于底部
    private TextView mTvImageSpan;//图片
    private TextView mTvRelativeSize;//相对字体
    private TextView mTvScaleX;//基于X轴缩放
    private TextView mTvStyle;//字体样式：粗体，斜体
    private TextView mTvSupscript;//上标
    private TextView mTvTextApprence;//字体、大小、样式和颜色
    private TextView mTvTypeFace;//文本字体
    private TextView mTvUrl;//超链接
    private TextView mTvSubsript;//下标

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_span);

        initParamsAndValues();

        initView();

        setSpan();
    }

    private void initParamsAndValues() {
        mContext = this;
    }

    private void initView() {
        mTvForegroundColor = findViewById(R.id.tv_foreground_color);
        mTvBackgroundColor = findViewById(R.id.tv_background_color);
        mTvClickable = findViewById(R.id.tv_clickable);
        mTvBlueMaskFilter = findViewById(R.id.tv_blur_mask_filter);
        mTvStrikeThrough = findViewById(R.id.tv_strike_through);
        mTvUnderlineSpan = findViewById(R.id.tv_underline);
        mTvAbsoluteSize = findViewById(R.id.tv_absolute_size);
        mTvDynamicDrawable = findViewById(R.id.tv_dynamic_drawable);
        mTvImageSpan = findViewById(R.id.tv_image_span);
        mTvRelativeSize = findViewById(R.id.tv_relative_size);
        mTvScaleX = findViewById(R.id.tv_scale_x);
        mTvStyle = findViewById(R.id.tv_style);
        mTvSupscript = findViewById(R.id.tv_supscript);
        mTvTextApprence = findViewById(R.id.tv_text_appearance);
        mTvTypeFace = findViewById(R.id.tv_type_face);
        mTvUrl = findViewById(R.id.tv_url);
        mTvSubsript = findViewById(R.id.tv_subsript);
    }

    private void setSpan() {
        setForegroundColorSpan();
        setBackgroundColorSpan();
        setClickableSpan();
        setBlurMaskFilter();
        setStrikethrough();
        setUnderlineSpan();
        setAbsoluteSizeSpan();
        setDynamicDrawable();
        setImageSpan();
        setRelativeSize();
        setScaleX();
        setStyle();
        setSupscript();
        setTextApperance();
        setTypeFace();
        setUrlSpan();
        setSubScrpit();
    }

    private void setForegroundColorSpan() {
        //设置字体颜色 + 插入字体的颜色
        //1、构造SpannableStringBuilder:它可以设置Sting的样式。
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(content);
        //2、构造Span(范围)
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#FF4040"));
        //3、给SpannableStringBuilder设置Span
        stringBuilder.setSpan(foregroundColorSpan, 1, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //❤：注意：插入的时候，会根据setSpan的第四个属性来决定是否影响插入数据的属性。
        stringBuilder.insert(1, "Hello");
        mTvForegroundColor.setText(stringBuilder);
    }

    private void setBackgroundColorSpan() {
        //设置背景颜色 + 插入的数据

        //1、构造SpannableStringBuilder对象。
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);
        //2、设置背景颜色Span
        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#FF4040"));
        //3、给SpannableStringBuilder设置Span，设置样式
        spannableStringBuilder.setSpan(backgroundColorSpan, 0, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        //❤：注意：插入的时候，会根据setSpan的第四个属性来决定是否影响插入数据的属性。
        spannableStringBuilder.insert(3, "hello");

        mTvBackgroundColor.setText(spannableStringBuilder);
    }

    private void setClickableSpan() {
        //设置文本可点击，有点击事件 + 两个Span的重合（可以同时设置多个Span（多个范围））

        //1、构造SpannableStringBuilder对象
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);
        //2、构造Span
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(mContext, "可点击", Toast.LENGTH_LONG).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                //去掉可点击文字的下划线:true:显示下划线，false:不显示下划线
                ds.setUnderlineText(true);
            }
        };
        //3、给SpannablStringBuilder设置可点击Span（设置可点击的范围）:可点击，有点击事件。
        spannableStringBuilder.setSpan(clickableSpan, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        //4、设置背景颜色Span(颜色背景的范围)
        BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#FF4040"));
        spannableStringBuilder.setSpan(backgroundColorSpan, 0, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        //设置此方法后，点击事件才能生效
        mTvClickable.setMovementMethod(LinkMovementMethod.getInstance());
        mTvClickable.setText(spannableStringBuilder);
    }

    private void setBlurMaskFilter() {
        //设置模糊效果

        //1、构造SpannableStringBuilder对象。
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、设置模糊Span，设置模糊的范围。
        MaskFilter filter = new BlurMaskFilter(4.0f, BlurMaskFilter.Blur.OUTER);
        MaskFilterSpan maskFilterSpan = new MaskFilterSpan(filter);

        //3、给SpannableStringBuilder设置范围(span)。
        spannableStringBuilder.setSpan(maskFilterSpan, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvBlueMaskFilter.setText(spannableStringBuilder);
    }

    private void setStrikethrough() {
        //删除线

        //1、构造SpannableStringBuilder
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(content);
        //2、构造下划线的Span（范围）
        StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
        //3、给SpanStringBuilder设置下划线的范围
        stringBuilder.setSpan(strikethroughSpan, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvStrikeThrough.setMovementMethod(LinkMovementMethod.getInstance());
        mTvStrikeThrough.setText(stringBuilder);
    }

    private void setUnderlineSpan() {
        //下划线

        //1、构造SpannableStringBuilder。
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、构造下划线的对象。
        UnderlineSpan un = new UnderlineSpan();

        //3、给SpannableStringBuilder设置下划线效果。
        spannableStringBuilder.setSpan(un, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvUnderlineSpan.setText(spannableStringBuilder);
    }

    private void setAbsoluteSizeSpan() {
        //设置绝对字体

        //1、构造SpannableStringBuilder对象。
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、字体绝对大小范围
        AbsoluteSizeSpan ab = new AbsoluteSizeSpan(30, true);

        //3、给SpannableStringBuilder设置文本字体绝对的大小
        spannableStringBuilder.setSpan(ab, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvAbsoluteSize.setText(spannableStringBuilder);
    }

    private void setDynamicDrawable() {
        //将固定文字设置为图片

        //1、构造SpannableStringBuilder对象
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、构造动态的图像范围
        DynamicDrawableSpan ab = new DynamicDrawableSpan() {
            @Override
            public Drawable getDrawable() {
                Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
                drawable.setBounds(0, 0, 50, 50);
                return drawable;
            }
        };

        //3、给SpannableStringBuilder设置图像的范围
        spannableStringBuilder.setSpan(ab, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvDynamicDrawable.setText(spannableStringBuilder);
    }

    private void setImageSpan() {
        //设置图片

        //1、构造SpannableStringBuilder
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(content);

        //2、生成图像范围
        Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher_round);
        drawable.setBounds(0, 0, 100, 100);
        ImageSpan ab = new ImageSpan(drawable);

        //3、给SpannableStringBuilder设置图像范围
        stringBuilder.setSpan(ab, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvImageSpan.setText(stringBuilder);
    }

    private void setRelativeSize() {
        //相对字体大小（RelativeSizeSpan ）

        //1、构造SpannableStringBuilder
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、构造相对字体大小范围
        RelativeSizeSpan ab = new RelativeSizeSpan(3.0f);

        //3、给文本字体绝对的大小
        spannableStringBuilder.setSpan(ab, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvRelativeSize.setText(spannableStringBuilder);

    }

    private void setScaleX() {
        //基于X的缩放

        //1、构造SpannableStringBuilder
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、X缩放范围的对象
        ScaleXSpan ab = new ScaleXSpan(3.0f);

        //3、给spannableStringBuilder设置X缩放的范围
        spannableStringBuilder.setSpan(ab, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvScaleX.setText(spannableStringBuilder);
    }

    private void setStyle() {
        //设置粗体，斜体样式

        //1、构造SpannableStringBuilder对象。
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、构造样式StyleSpan:可以设置粗体，斜体等。
        StyleSpan ab = new StyleSpan(Typeface.BOLD_ITALIC);

        //3、给SpannableStringBuilder设置Span
        spannableStringBuilder.setSpan(ab, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvStyle.setText(spannableStringBuilder);
    }

    private void setSupscript() {
        //设置上标

        //1、构造SpannableStringBuilder对象。
        String content = "22的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、构造一个上标的范围,表示哪个位置的东西在上标的地方。
        SuperscriptSpan ab = new SuperscriptSpan();

        //3、给Spannable设置上标的范围。
        spannableStringBuilder.setSpan(ab, 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvSupscript.setText(spannableStringBuilder);
    }


    private void setTypeFace() {
        //设置文本字体

        //1、构造SpannableStringBuilder对象
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、构造字体的范围对象。"monospace", "serif", and "sans-serif"
        TypefaceSpan ab = new TypefaceSpan("serif");

        //3、给SpannableStringBuilder设置影响范围。
        spannableStringBuilder.setSpan(ab, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvTypeFace.setText(spannableStringBuilder);
    }

    private void setTextApperance() {
        //字体、大小、样式和颜色

        //1、构造SpannableStringBuilder对象。
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、设置自定义文本样式范围：以后这个地方可以使用我们自己的样式
        TextAppearanceSpan ab = new TextAppearanceSpan(this, android.R.style.TextAppearance_Medium);

        //3、给SpannableStringBuilder设置范围
        spannableStringBuilder.setSpan(ab, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvTextApprence.setText(spannableStringBuilder);
    }


    private void setUrlSpan() {
        //超链接

        //1、构造SpannableStringBuilder对象。
        String content = "预祝党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);

        //2、构造超链接范围对象。
        URLSpan ab = new URLSpan("http://www.baidu.com");

        //3、给SpannableStringBuilder设置超链接范围对象。
        spannableStringBuilder.setSpan(ab, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvUrl.setMovementMethod(new LinkMovementMethod());
        mTvUrl.setText(spannableStringBuilder);
    }

    private void setSubScrpit() {
        //下标

        //1、构造SpannableStringBuilder对象。
        String content="log10党的十九大完美谢慕";
        SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder(content);

        //2、构造下标范围对象。
        SubscriptSpan ab = new SubscriptSpan();

        //3、给StringStringBuilder设置下标影响范围。
        spannableStringBuilder.setSpan(ab,3,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTvSubsript.setMovementMethod(new LinkMovementMethod());
        mTvSubsript.setText(spannableStringBuilder);
    }
}
